package org.korifey.ffalg;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Iterator;

import org.junit.Test;

public class ProbabilitiesTest {

	@Test
	public void testGenerateCombinations() {
		Iterator<int[]> it = Probabilities.generateCombinations(3, 2);
		assertTrue(it.hasNext());
		assertTrue(Arrays.equals(it.next(), new int[]{0,1}));
		assertTrue(Arrays.equals(it.next(), new int[]{0,2}));
		assertTrue(Arrays.equals(it.next(), new int[]{1,2}));
		assertFalse(it.hasNext());
	}
	
	@Test
	public void testGenerateCombinationsOneElemt() {		
		Iterator<int[]> it = Probabilities.generateCombinations(3,1);
		assertTrue(it.hasNext());
		assertTrue(Arrays.equals(it.next(), new int[]{0}));
		assertTrue(Arrays.equals(it.next(), new int[]{1}));
		assertTrue(Arrays.equals(it.next(), new int[]{2}));
		assertFalse(it.hasNext());
	}
	
	@Test
	public void testGenerateCombinationsAll() {		
		Iterator<int[]> it = Probabilities.generateCombinations(3,3);
		assertTrue(it.hasNext());
		assertTrue(Arrays.equals(it.next(), new int[]{0, 1, 2}));		
		assertFalse(it.hasNext());
	}
	
	@Test
	public void testGenerateCombinations42() {		
		Iterator<int[]> it = Probabilities.generateCombinations(4,2);
		assertTrue(it.hasNext());
		assertTrue(Arrays.equals(it.next(), new int[]{0, 1}));		
		assertTrue(Arrays.equals(it.next(), new int[]{0, 2}));
		assertTrue(Arrays.equals(it.next(), new int[]{1, 2}));
		assertTrue(Arrays.equals(it.next(), new int[]{0, 3}));
		assertTrue(Arrays.equals(it.next(), new int[]{1, 3}));
		assertTrue(Arrays.equals(it.next(), new int[]{2, 3}));
		assertFalse(it.hasNext());
	}
	
	@Test
	public void testGenerateCombinationsEmpty() {		
		Iterator<int[]> it = Probabilities.generateCombinations(4,0);
		assertTrue(it.hasNext());
		assertTrue(Arrays.equals(it.next(), new int[]{}));		
		assertFalse(it.hasNext());
	}
	
	@Test
	public void testFactRadixToPermutation() {
		assertArrayEquals(new int[]{0}, Probabilities.factRadixToPermutation(0, 1));
						
		assertArrayEquals(new int[]{0, 1}, Probabilities.factRadixToPermutation(0, 2));
		assertArrayEquals(new int[]{1, 0}, Probabilities.factRadixToPermutation(1, 2));
		
		assertArrayEquals(new int[]{0, 1, 2}, Probabilities.factRadixToPermutation(0, 3));
		assertArrayEquals(new int[]{0, 2, 1}, Probabilities.factRadixToPermutation(1, 3));
		assertArrayEquals(new int[]{1, 0, 2}, Probabilities.factRadixToPermutation(2, 3));
		assertArrayEquals(new int[]{1, 2, 0}, Probabilities.factRadixToPermutation(3, 3));
		assertArrayEquals(new int[]{2, 0, 1}, Probabilities.factRadixToPermutation(4, 3));
		assertArrayEquals(new int[]{2, 1, 0}, Probabilities.factRadixToPermutation(5, 3));
	}
	
	@Test
	public void testGeneratePermutation() {
		Iterator<int[]> it = Probabilities.generatePermutations(1);
		assertArrayEquals(new int[]{0}, it.next());
		assertFalse(it.hasNext());
		
		it = Probabilities.generatePermutations(2);
		assertArrayEquals(new int[]{0, 1}, it.next());
		assertArrayEquals(new int[]{1, 0}, it.next());
		assertFalse(it.hasNext());
		
		it = Probabilities.generatePermutations(3);
		assertArrayEquals(new int[]{0, 1, 2}, it.next());
		assertArrayEquals(new int[]{0, 2, 1}, it.next());
		assertArrayEquals(new int[]{1, 0, 2}, it.next());
		assertArrayEquals(new int[]{1, 2, 0}, it.next());
		assertArrayEquals(new int[]{2, 0, 1}, it.next());
		assertArrayEquals(new int[]{2, 1, 0}, it.next());
		assertFalse(it.hasNext());
	}
}

